﻿Imports FuncClasses
Imports System.Threading.Tasks

Public Class AuftragRibbon
    Public Event SavePDF(path As String, source As AuftragRibbon)
    Public Event PrintPDF(source As AuftragRibbon)
    Private _parent As Window1
    Public _selecteddate As Date = Date.Today

    Public Sub New(ByRef mainwindow As Window1)
        Me.Resources.MergedDictionaries.Clear()
        InitializeComponent()
        _parent = mainwindow
        Me.Resources.MergedDictionaries.Add(Dictionaries.GetOrderControlLocalization)
    End Sub

    ''' <summary>
    ''' Call this to request closing the actual order and its controls
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RequestOrderClosing()
        RaiseEvent SaveAndExit(Me)
    End Sub

    ''' <summary>
    ''' Call this to request the pdf export methode
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RequestPDFExport()
        buttonAuftragPDF2_Click(Nothing, Nothing)
    End Sub

    ''' <summary>
    ''' Call this to request printing the actual order
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RequestPrint()
        buttonAuftragDrucken2_Click(Nothing, Nothing)
    End Sub

    ''' <summary>
    ''' Call this if you want to add an article to the order
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub RequestAddArticle()
        RaiseEvent RequestArticle(Me)
    End Sub

    Public Event SaveAsPDFClicked(ByRef filenameToUse As String)
    Private Sub buttonAuftragPDF2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles buttonAuftragPDF2.Click
        Dim s As String = ""
        RaiseEvent SaveAsPDFClicked(s)
        ' Configure open file dialog box
        Dim dlg As New System.Windows.Forms.SaveFileDialog()
        dlg.Title = "PDF Speichern"
        dlg.FileName = s
        dlg.DefaultExt = ".pdf" ' Default file extension
        dlg.Filter = "PDF Dokument |*.pdf" ' Filter files by extension

        ' Process open file dialog box results
        If dlg.ShowDialog() = Forms.DialogResult.OK Then
            Try
                RaiseEvent SavePDF(dlg.FileName, Me)
                'currentLayout.Save(Auftrag, dlg.FileName)
            Catch ex As Exception
                MessageBox.Show(GetUIString("PDFExportError"), GetUIString("Error"), MessageBoxButton.OK, MessageBoxImage.Error)
            End Try
        End If
    End Sub

    Private Sub buttonAuftragDrucken2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles buttonAuftragDrucken2.Click
        Try
            RaiseEvent PrintPDF(Me)
        Catch ex As Exception
            MessageBox.Show(GetUIString("PDFExportError"), GetUIString("Error"), MessageBoxButton.OK, MessageBoxImage.Error)
        End Try
    End Sub

    'Public Event OwnCustomerChecked()
    ' ''' <summary>
    ' ''' Macht die Felder zum Eintragen eigener Adressdaten sichtbar
    ' ''' </summary>
    ' ''' <param name="sender"></param>
    ' ''' <param name="e"></param>
    ' ''' <remarks></remarks>
    'Private Sub buttonAuftragKundeEigen_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonAuftragKundeEigen.Checked
    '    RaiseEvent OwnCustomerChecked()
    'End Sub

    'Public Event OwnCustomerUnchecked()
    ' ''' <summary>
    ' ''' Macht die Felder zum Eingeben einer benutzerdefinierten Adresse wieder unsichtbar
    ' ''' </summary>
    ' ''' <param name="sender"></param>
    ' ''' <param name="e"></param>
    ' ''' <remarks></remarks>
    'Private Sub buttonAuftragKundeEigen_Unchecked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonAuftragKundeEigen.Unchecked
    '    RaiseEvent OwnCustomerUnchecked()
    'End Sub

    Public Event UserWantToChooseCustomer(source As AuftragRibbon)
    ''' <summary>
    ''' Mit einem Klick wird man zur Kundenübersicht weitergeleitet, und dann ein Kunde gewählt
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub buttonAuftragKundewählen_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonAuftragKundewählen.Click
        'buttonAuftragKundeEigen.IsChecked = False
        RaiseEvent UserWantToChooseCustomer(Me)
    End Sub

    Public Event QuickSave()
    ''' <summary>
    ''' Auftrag zwischenspeichern
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub buttonAuftragzwischenspeichern_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonAuftragzwischenspeichern.Click
        RaiseEvent QuickSave()
    End Sub

    Public Event SaveAndExit(source As AuftragRibbon)
    Private Sub buttonAuftragSpeichern_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonAuftragSpeichern.Click
        RaiseEvent SaveAndExit(Me)
    End Sub

    Public Event Abort(source As AuftragRibbon)
    Private Sub buttonAuftragAbbrechen_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonAuftragAbbrechen.Click
        RaiseEvent Abort(Me)
    End Sub

    Public Event ButtonRechnungChecked()
    Private Sub MIRechnung_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MIRechnung.Checked
        If MIRechnung.IsEnabled Then
            MILieferschein.IsChecked = False
            MIAngebot.IsChecked = False
            MIGutschrift.IsChecked = False
            RaiseEvent ButtonRechnungChecked()
        End If
    End Sub
    Public Event ButtonLieferscheinChecked()
    Private Sub MILieferschein_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MILieferschein.Checked
        If MIRechnung.IsEnabled Then
            MIRechnung.IsChecked = False
            MIAngebot.IsChecked = False
            MIGutschrift.IsChecked = False
            RaiseEvent ButtonLieferscheinChecked()
        End If
    End Sub
    Public Event ButtonAngebotChecked()
    Private Sub MIAngebot_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MIAngebot.Checked
        If MIRechnung.IsEnabled Then
            MILieferschein.IsChecked = False
            MIRechnung.IsChecked = False
            MIGutschrift.IsChecked = False
            RaiseEvent ButtonAngebotChecked()
        End If
    End Sub
    Public Event ButtonGutschriftChecked()
    Private Sub MIRGutschrift_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MIGutschrift.Checked
        If MIRechnung.IsEnabled Then
            MILieferschein.IsChecked = False
            MIAngebot.IsChecked = False
            MIRechnung.IsChecked = False
            RaiseEvent ButtonGutschriftChecked()
        End If
    End Sub

    Public Event RequestArticle(source As AuftragRibbon)
    Private Sub buttonAuftragArtikelhinzufügen_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonAuftragArtikelhinzufügen.Click
        RaiseEvent RequestArticle(Me)
    End Sub

    Public Event ArticleDeleteRequest()
    Private Sub buttonAuftragArtikellöschen_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles buttonAuftragArtikellöschen.Click
        RaiseEvent ArticleDeleteRequest()
    End Sub

    Public Event SpinnerValueChanged(ByRef value As Integer)
    ''' <summary>
    ''' Farbänderung, wenn Auftragsnummer schon vergeben
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub spinner_ValueChanged(sender As System.Object, e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Double)) Handles spinner.ValueChanged
        RaiseEvent SpinnerValueChanged(spinner.Value)
    End Sub

#Region "Datum definieren"
    Private calendar As Calendar
    Public Event AuftragDateChanged(datum As Datum)
    Private Sub ButtonDatum_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ButtonDatum.Click
        If calendar Is Nothing OrElse _parent.MainGrid.Children.Contains(calendar) = False Then
            calendar = New Calendar
            AddHandler calendar.SelectedDatesChanged, AddressOf Calendar1_SelectedDatesChanged
            calendar.DisplayDate = _selecteddate
            calendar.Name = "Kalender"

            calendar.Visibility = Windows.Visibility.Visible
            _parent.MainGrid.Children.Add(calendar)
            ' Return the offset vector for the object.
            Dim vector As Vector = VisualTreeHelper.GetOffset(ButtonDatum)

            ' Convert the vector to a point value.
            Dim p As New Point(vector.X, vector.Y)
            p.X += ButtonDatum.ActualWidth / 2
            p.X -= 180 / 2
            calendar.Margin = New Thickness(p.X, 0, 0, 0)
            calendar.HorizontalAlignment = Windows.HorizontalAlignment.Left
            calendar.VerticalAlignment = Windows.VerticalAlignment.Top
        Else
            _parent.MainGrid.Children.Remove(calendar)
        End If
    End Sub

    Private Sub Calendar1_SelectedDatesChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        Dim datum As Date = calendar.SelectedDate.Value
        _selecteddate = datum
        RaiseEvent AuftragDateChanged(New Datum(datum))
        ButtonDatum.Header = "Datum " + New Datum(datum).ToString
        _parent.MainGrid.Children.Remove(calendar)
    End Sub

#End Region

    Private Function GetUIString(key As String)
        Try
            Return Me.FindResource(key).ToString
        Catch ex As Exception
            Debug.WriteLine("Resource not found in OrderControl: " + key)
            Return key
        End Try
    End Function

    Private Sub RibbonTabItem_KeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyDown
        If e.Key = Key.S AndAlso (Keyboard.Modifiers And (ModifierKeys.Control Or ModifierKeys.Shift)) = (ModifierKeys.Control Or ModifierKeys.Shift) Then
            RequestOrderClosing()
        End If
        If e.Key = Key.S AndAlso (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
            RaiseEvent QuickSave()
        End If
        If e.Key = Key.P AndAlso (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
            RequestPrint()
        End If
        If e.Key = Key.E AndAlso (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
            RequestPDFExport()
        End If
        If e.Key = Key.N AndAlso (Keyboard.Modifiers And ModifierKeys.Control) = ModifierKeys.Control Then
            RequestAddArticle()
        End If
    End Sub
End Class
